package com.youdoneed.community.forum.service;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.youdoneed.base.application.AppException;
import com.youdoneed.community.common.utils.StringUUIdUtil;
import com.youdoneed.community.forum.dao.ForumuserattentionmessagelabelrelMapper;
import com.youdoneed.community.forum.entity.Forummessagelabel;
import com.youdoneed.community.forum.entity.Forumuserattentionmessagelabelrel;

/**
 * 个人关注标签记录服务层接口
 *
 * @author lid
 * @date 2017.6.15
 */
@Service
public class ForumuserattentionmessagelabelrelService {

    private final Logger logger = LoggerFactory.getLogger(ForumuserattentionmessagelabelrelService.class);

    @Autowired
    ForumuserattentionmessagelabelrelMapper mapper;

    /**
     * 保存关注标签记录
     *
     * @param rel
     * @return int
     * @author lid
     * @date 2017.6.15
     */
    @Transactional
    public int saveForumuserattentionmessagelabelrel(Forumuserattentionmessagelabelrel rel) {
        logger.info("saveForumuserattentionmessagelabelrel");
        if (null == rel) {
            throw new AppException("rel参数为空！");
        }

        rel.setForumUserAttentionMessageLabelRelId(StringUUIdUtil.getUUID());
        rel.setCreateDate(new Date());

        try {
            return mapper.insert(rel);
        } catch (Exception e) {
            logger.info("保存关注标签记录失败！" + e);
            return 0;
        }
    }

    /**
     * 删除关注标签记录
     *
     * @param id
     * @return int
     * @author lid
     * @date 2017.6.15
     */
    @Transactional
    public int deleteForumuserattentionmessagelabelrel(String id) {
        logger.info("deleteForumuserattentionmessagelabelrel");
        if (!StringUtils.hasText(id)) {
            throw new AppException("id参数为空！");
        }

        try {
            return mapper.deleteByPrimaryKey(id);
        } catch (Exception e) {
            logger.info("删除关注标签记录失败！" + e);
            return 0;
        }
    }

    /**
     * 根据userId获取当前用户关注的标签
     *
     * @param userId
     * @return List<Forummessagelabel>
     * @author lid
     * @date 2017.6.15
     */
    @Transactional
    public List<Forummessagelabel> getUserAttentionLabelsByUserId(String userId) {
        logger.info("getUserAttentionLabelsByUserId");
        if (!StringUtils.hasText(userId)) {
            throw new AppException("userId参数为空！");
        }
        try {
            List<Forummessagelabel> listForummessagelabel = mapper.getUserAttentionLabelsByUserId(userId);
            return listForummessagelabel;
        } catch (Exception e) {
            logger.info("根据userId获取当前用户关注的标签失败！" + e);
            return null;
        }
    }
}
